home *** CD-ROM | disk | FTP | other *** search
- moveq #1,d7
- move.l #9999,d0
- l: bsr Random
- dbf d0,l
- rts
-
- ***********************************************************
- * Input
- * d7 : seed
- *
- * Output
- * d7 : pseudo-random number
- *
- * Uses
- * d2-d7
-
- Random: move.l d7,d6 ; copy Rand(i)
- move.l #127773,d2 ; sythetic modulus
- bsr.b Div ; divide d6 by 127773
- move.l d4,d5 ; copy to K1
- muls #-2836,d5 ; d5 = -2836 * K1
- mulu #42591,d4 ; multiply d4 by 127773
- move.l d4,d6
- add.l d4,d4
- add.l d6,d4
- sub.l d4,d7 ; d7 = Rand(i) - K1 * 127773
-
- moveq #4,d4 ; loop counter
- .loop: move.l d7,d6 ; multiply d7 by 16807
- lsl.l #3,d7
- sub.l d6,d7
- dbf d4,.loop
-
- add.l d5,d7 ; d7 = Rand(i + 1)
- bpl.b .exit
- addi.l #$7fffffff,d7 ; normalize negative result
- .exit: rts
-
- ***********************************************************
-
- Div: add.l d6,d6 ; shift out unused bit
- moveq #0,d4 ; quotient
- moveq #14,d3 ; counter
- move.w d6,d5 ; save low word of Rand(i)
- swap d6
- andi.l #$ffff,d6 ; d6 = Rand(i) DIV 2^15
-
- .loop: add.w d4,d4 ; line up quotient and..
- add.w d5,d5 ; dividend
- addx.l d6,d6 ; shift in bit of low word
- cmp.l d2,d6 ; trial subtraction
- bmi.b .exit
- sub.l d2,d6 ; real subtraction
- addq.w #1,d4 ; put 1 in quotient
- .exit: dbf d3,.loop ; loop
- rts
-